<?php
namespace App\Controllers\Order;
use App\Models\Order\OrderComment;
use App\Models\Order\Order;
use App\Models\Store\Store;
use App\Models\Goods\Goods;
use App\Libs\Order\OrderComment as orderCom;

/**
*	设置订单评价
*/
class SetOrderCommon extends \App\Libs\Base\BaseAction
{
    protected function beforeRun() {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }
	
	public function execute(){
		$user = $this->session->user;
		//获取信息
		$orderSn = $this->request->get('orderSn');//订单号
		$num = (int)$this->request->get('num');//评价分数
		$content = $this->request->get('content');//内容
		$images = $this->request->get('images');//图片
		$niming = (int)$this->request->get('niming');//是否匿名
		
		//验证
		if( empty( $orderSn ) ){
			return \App\Libs\Views\Json::view(1, '订单号不能为空', Null );
		}
		if( !in_array( $num , [1,2,3,4,5] ) ){
			return \App\Libs\Views\Json::view(1, '请选择正确的评分', Null );
		}
		if( empty( $content ) ){
			return \App\Libs\Views\Json::view(1, '评价内容不能为空', Null );
		}
		if( !in_array( $niming , [0,1] ) ){
			$niming = 0;
		}
		
		//数据库验证
			//判断订单是否存在
		$orderInfo = Order::find(array(
			' order_sn = :oid: and uid = :uid: and status = 5',
			'bind' => array( 'oid'=>$orderSn , 'uid' => $user->id )
		));
		if( empty( $orderInfo ) ){
			return \App\Libs\Views\Json::view(1, '订单号不存在', Null );
		}
			//判断是否已经评价
		$commonHave = OrderComment::find(array(
			' oid = :oid: ',
			'bind' => array( 'oid'=>$orderSn ),
		));
			//获取店铺信息
		$goodsInfo = Goods::find( array( 
			' id = :gid:',
			'bind' => array( 'gid' => $orderInfo->gid )
		) );
		if( empty( $goodsInfo ) ){
			return \App\Libs\Views\Json::view(1, '店铺不存在，请联系工作人员', Null );
		}
		
		if( empty( $commonHave ) ){
			//未评价 写入
			$orderCommon = new OrderComment();
			$orderCommon->uid = $user->id;
			$orderCommon->oid = $orderSn;
			$orderCommon->gid = $orderInfo->gid ;
			$orderCommon->sid = $goodsInfo->sid;
			$orderCommon->anonymity = $niming;
			$orderCommon->num = $num;
			$orderCommon->content = $content;
			$orderCommon->ctime=date('Y-m-d H:i:s');
			if( $orderCommon->save() !== false ){
				//商品点评数修改
				$goodsInfo->goods_grade += 1;
				$goodsInfo->save();
				
				//店铺积分计算
				$score = orderCom::OrderCommentAll( $goodsInfo->sid );
				if( !empty( $score ) ){
					$store = Store::find( array( 
						' id = :sid:',
						'bind'=>array( 'sid' => $goodsInfo->sid )
					) );
					$store->store_grade = floor( $score );
					$store->save();
				}
				
				//修改为已评价
				$orderInfo->status = 8;
				$orderInfo->save();
				
				//图片处理
				if( !empty( $images ) ){
					$orderCommonImg = new OrderCommentImg();
					$orderCommonImg->ocid = $orderCommon->id;
					$imagesArr = json_decode( $images , true );
					foreach( $imagesArr as $k=>$v ){
						$orderCommonImg->img_url = $v['url'];
						$orderCommonImg->save;
					}
				}
				return \App\Libs\Views\Json::view(0, '评价成功', Null );	
			}else{
				return \App\Libs\Views\Json::view(1, '评价保存失败，请刷新重新评价', Null );
			}
		}else{
			//已评价 退出 
			return \App\Libs\Views\Json::view(1, '已评价订单，不能再次评价', Null );
		}
	}
}
